//
//  LYPlayMusicViewController.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/6/9.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYPlayMusicViewController.h"
#import "LYSongTableViewCell.h"
#import "LYMusicBtnVCSingleton.h"

@interface LYPlayMusicViewController () <UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate,LYSongTableViewCelldelegate,UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *backgroundScrollview;

//   主模块的部分
@property (strong, nonatomic) UIImageView * bkgimageView;
@property (strong, nonatomic) UIView *littleView;
@property (strong, nonatomic) UIScrollView * tableScrollView;
@property (strong, nonatomic) UITableView *leftTableView;
@property (strong, nonatomic) UITableView *rightTableView;

//  控制模块的View
@property (weak, nonatomic) IBOutlet UIView *controlView;

//  电台开始时间、结束时间和滑条
@property (weak, nonatomic) IBOutlet UILabel *startTime;
@property (weak, nonatomic) IBOutlet UISlider *progessBar;
@property (weak, nonatomic) IBOutlet UILabel *endTime;

//  开始播发、上一首、下一首按钮
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UIButton *lastBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@end

@implementation LYPlayMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    [self initInterface];
    
}

-(void)backAction{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

//  初始化界面排布
- (void)initInterface {
   
    
    //  导航栏的设置
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    label.text = @"我的电台";
    [label setFont:[UIFont systemFontOfSize:18]];
    [label setTextColor:[UIColor whiteColor]];
    self.navigationItem.titleView  = label;
    
    //  上面的图片
    self.bkgimageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ding"]];
    self.bkgimageView.frame                = CGRectMake(0, 0, SCREEN_WIDTH, 315);
    
    
    //  中间的两个切换按钮
    UIView * view                              = [[UIView alloc]initWithFrame:CGRectMake(0, 315, SCREEN_WIDTH, 55)];
    
    UIButton * playBtn                      = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2, 53)];
    [playBtn setTitle:@"即将播放" forState:UIControlStateNormal];
    [playBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [playBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [playBtn addTarget:self action:@selector(clickWillPlay) forControlEvents:UIControlEventTouchDown];
    [view addSubview:playBtn];
    
    UIButton * didPlayBtn                 = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, 53)];
    [didPlayBtn setTitle:@"已播内容" forState:UIControlStateNormal];
    [didPlayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [didPlayBtn addTarget:self action:@selector(clickDidPlay) forControlEvents:UIControlEventTouchDown];
    [didPlayBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [view addSubview:didPlayBtn];
    
    self.littleView                                       = [[UIView alloc]initWithFrame:CGRectMake(48, 53, 90, 2)];
    self.littleView.backgroundColor          = [UIColor blueColor];
    [view addSubview:self.littleView];
    
    
    //  切换按钮下面的scrollView
    self.tableScrollView      = [[UIScrollView alloc]initWithFrame:CGRectMake(0, view.frame.origin.y+view.frame.size.height, SCREEN_WIDTH, self.backgroundScrollview.frame.size.height-view.frame.size.height-64)];
    self.tableScrollView.contentSize         = CGSizeMake(self.tableScrollView.frame.size.width*2, 0);
    self.tableScrollView.pagingEnabled    = YES;
    self.tableScrollView.showsHorizontalScrollIndicator = NO;
    self.tableScrollView.delegate              = self;
    self.tableScrollView.tag                       = 1003;
    self.tableScrollView.bounces              = NO;
    
    self.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.tableScrollView.frame.size.height)];
    self.leftTableView.delegate                 = self;
    self.leftTableView.dataSource             = self;
    self.leftTableView.backgroundColor  = [UIColor yellowColor];
    self.leftTableView.tag                         =  1001;
    
    //添加拖动手势
//    UISwipeGestureRecognizer *panRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
//    panRecognizer.delegate = self;
//    [panRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
//    [self.leftTableView addGestureRecognizer:panRecognizer];
    
    UISwipeGestureRecognizer * recognizer;
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.leftTableView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.leftTableView addGestureRecognizer:recognizer];
//    self.leftTableView.bounces                = NO;
    
    self.rightTableView       = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, self.tableScrollView.frame.size.height)];
    self.rightTableView.delegate               = self;
    self.rightTableView.dataSource           = self;
    self.rightTableView.tag                         = 1002;
    self.rightTableView.bounces               = NO;
    
    
    [self.tableScrollView addSubview:self.leftTableView];
    [self.tableScrollView addSubview:self.rightTableView];
    
    
    //  背景scrollView的设置
    self.backgroundScrollview.tag                 =1000;
    self.backgroundScrollview.bounces        = NO;
    self.backgroundScrollview.delegate       = self;
    self.backgroundScrollview.contentSize  = CGSizeMake(SCREEN_WIDTH, self.backgroundScrollview.frame.size.height+315-64);
    
    [self.progessBar setThumbImage:[UIImage imageNamed:@"circle_img"] forState:UIControlStateNormal];
    //  添加到背景scrollView
    [self.backgroundScrollview addSubview:self.bkgimageView];
    [self.backgroundScrollview addSubview:view];
    [self.backgroundScrollview addSubview:self.tableScrollView];
}

#pragma mark - <UIScrollViewDelegate>

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (self.backgroundScrollview.contentOffset.y <= 250) {
        self.leftTableView.scrollEnabled = NO;
        self.rightTableView.scrollEnabled = NO;
    }else{
        self.leftTableView.scrollEnabled = YES;
        self.rightTableView.scrollEnabled = YES;
        if (self.leftTableView.contentOffset.y<0|| self.rightTableView.contentOffset.y<0) {
            self.leftTableView.scrollEnabled = NO;
            self.rightTableView.scrollEnabled =  NO;
        }
    }

    NSLog(@"%ld",scrollView.tag);
    NSLog(@"%f",self.backgroundScrollview.contentOffset.y);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"%@",scrollView.gestureRecognizers);
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYSongTableViewCell *cell = [[LYSongTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"'cell"];
    cell.delegate = self;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 67;
}

#pragma mark - <LYSongTableViewCelldelegate,UIActionSheetDelegate>

-(void)clickSetBtn{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"电台节目"
                                  delegate:self
                                  cancelButtonTitle:@"删除"
                                  destructiveButtonTitle:@"优先"
                                  otherButtonTitles:@"评论", @"打标签",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSLog(@"优先");
    }else if (buttonIndex == 1) {
        NSLog(@"评论");
    }else if(buttonIndex == 2) {
        NSLog(@"打标签");
    }else if(buttonIndex == 3) {
        NSLog(@"删除");
    }
}

#pragma mark - 事件的处理

-(void)back{
    [self dismissViewControllerAnimated:YES completion:NULL];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [LYMusicBtnWindowSingleton sharedMusicBtnWindowSingleton:self].hidden = NO;
    });
}

-(void)clickWillPlay{
    [UIView animateWithDuration:0.5 animations:^{
        self.tableScrollView.contentOffset = CGPointMake(0, 0);
        self.littleView.frame =CGRectMake(48, 53, 90, 2);
    }];
}
-(void)clickDidPlay{
    [UIView animateWithDuration:0.5 animations:^{
        self.tableScrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width, 0);
        self.littleView.frame =CGRectMake([UIScreen mainScreen].bounds.size.width-138, 53, 90, 2);
    }];
    }
//  下方控制模块的事件监听
- (IBAction)sliderValueChanged:(id)sender {
    NSLog(@"%s",__func__);
}

- (IBAction)clickLastSong:(id)sender {
    NSLog(@"%s",__func__);
}

- (IBAction)clickPlaySong:(id)sender {
    NSLog(@"%s",__func__);
   
}

- (IBAction)clickNextSong:(id)sender {
    NSLog(@"%s",__func__);
}

//  手势的事件处理
- (void)panGesture:(UISwipeGestureRecognizer *)gestureRecognizer {
    
    NSLog(@"sheklfj");
 
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    if(recognizer.direction == UISwipeGestureRecognizerDirectionDown) {
        NSLog(@"swipe down");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionUp) {
        NSLog(@"swipe up");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"swipe left");
    }
    if(recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"swipe right");
    }
}


@end
